package com.liuyu.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {
    //    @RabbitListener(queues = "simple.queue")
    public void receive(String message) {
        log.info("spring 接收到消息:{}", message);
    }

    @RabbitListener(queues = "simple.queue")
    public void receiveByConverter(Map<String, Object> message) {
        log.info("spring 接收到消息:{}", message);
    }


    @RabbitListener(queues = "work.queue")
    public void receive2(String message) throws InterruptedException {
        log.info("工作队列1 接收到消息:{}", message);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "work.queue")
    public void receive3(String message) throws InterruptedException {
        log.info("工作队列2 接收到消息:{}", message);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void receive4(String message) throws InterruptedException {
        log.info("fanout队列1 接收到消息:{}", message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void receive5(String message) throws InterruptedException {
        log.info("fanout队列2 接收到消息:{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "direct.exchange", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void receive6(String message) throws InterruptedException {
        log.info("red,blue接收到消息:{}", message);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "direct.exchange", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void receive7(String message) throws InterruptedException {
        log.info("red,yellow接收到消息:{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(name = "topic.exchange", type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void receive8(String message) throws InterruptedException {
        log.info("prefix = china队列接收到消息:{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "topic.exchange", type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void receive9(String message) throws InterruptedException {
        log.info("subfix = news队列接收到消息:{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "confirm.queue1"),
            exchange = @Exchange(name = "confirm.callback.exchange", type = ExchangeTypes.DIRECT),
            key = "confirm"
    ))
    public void receive10(String message) throws InterruptedException {
        log.info("confirm.queue1:{}", message);
        //模拟异常
//        int i = 1 / 0;
        log.info("confirm.queue2:{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dl.queue", durable = "true"),
            exchange = @Exchange(name = "dl.direct", durable = "true"),
            key = "dl"
    ))
    public void receive11(String message) throws InterruptedException {
        log.info("监听死信队列得到消息:{}", message);
    }

    /**
     * 监听延时消息，如果不监听延时消息当有消息进入时则会进入到该延时队列绑定的死信交换机中，这里不监听延时队列测试消息超时到达死信队列的场景
     * @param message 消息
     * @throws InterruptedException
     */
//    @RabbitListener(queues = "ttl.queue")
//    public void receive12(String message) throws InterruptedException {
//        log.info("监听到延时消息:{}", message);
//    }

    /**
     * 声明延时队列
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.exchange", durable = "true", delayed = "true"),
            key = "delay"
    ))
    public void receive12(String message) throws InterruptedException {
        log.info("延时队列接收到消息,消息:{}", message);
    }

    /**
     * 惰性队列
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "lazy.queue", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")), //声明惰性队列
            exchange = @Exchange(name = "lazy.exchange", durable = "true"),
            key = "lazy"
    ))
    public void receive13(String message) throws InterruptedException {
        log.info("惰性队列接收到消息,消息:{}", message);
    }

}
